// import { login, logout, getInfo } from '@/api/login'
import { getToken, setToken, removeToken } from '@/utils/auth'
import request from "@/utils/request";

const user = {
  state: {
    token: getToken(),
    name: '',
    avatar: '',
    roles: [],
    permissions: []
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    SET_PERMISSIONS: (state, permissions) => {
      state.permissions = permissions
    }
  },

  actions: {
    // 登录
    Login({ commit }, userForm) {
      const username = userForm.username.trim()
      const password = userForm.password

      return new Promise((resolve, reject) => {


        request({
          method: 'get',
          url: '/login?username=' + username + '&password=' + password,
        }).then(ret => {

          let token = ret.data;

          setToken(token) ;//存储到浏览器中 持久
          commit('SET_TOKEN', token);//存储到vuex 对象中的token的状态
          resolve()

        }).catch(error=>{
          reject();
        })


      })
    },


  }
}

export default user
